import tkinter as tk
from tkinter import colorchooser, ttk

class DrawingApp:
    def __init__(self, root):
        self.root = root
        self.root.title("简易绘画程序1")
        self.root.geometry("800x600")
        
        # 设置默认参数
        self.brush_color = "black"
        self.brush_size = 5
        self.eraser_size = 20
        self.is_drawing = False
        
        # 创建画布
        self.canvas = tk.Canvas(root, bg="white")
        self.canvas.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        
        # 创建工具栏
        self.create_toolbar()
        
        # 绑定鼠标事件
        self.canvas.bind("<Button-1>", self.start_drawing)
        self.canvas.bind("<B1-Motion>", self.draw)
        self.canvas.bind("<ButtonRelease-1>", self.stop_drawing)
        
        # 存储上一个点的坐标
        self.last_x = None
        self.last_y = None

    def create_toolbar(self):
        """创建工具栏"""
        toolbar = ttk.Frame(self.root)
        toolbar.pack(fill=tk.X, padx=5, pady=5)
        
        # 颜色选择按钮
        color_btn = ttk.Button(toolbar, text="选择颜色", command=self.choose_color)
        color_btn.pack(side=tk.LEFT, padx=2)
        
        # 画笔大小选择
        size_frame = ttk.Frame(toolbar)
        size_frame.pack(side=tk.LEFT, padx=5)
        
        ttk.Label(size_frame, text="画笔大小:").pack(side=tk.LEFT)
        self.size_var = tk.IntVar(value=self.brush_size)
        size_spinbox = ttk.Spinbox(size_frame, from_=1, to=50, 
                                 textvariable=self.size_var, width=5,
                                 command=self.update_brush_size)
        size_spinbox.pack(side=tk.LEFT)
        
        # 橡皮擦按钮
        eraser_btn = ttk.Button(toolbar, text="橡皮擦", command=self.use_eraser)
        eraser_btn.pack(side=tk.LEFT, padx=2)
        
        # 清空画布按钮
        clear_btn = ttk.Button(toolbar, text="清空画布", command=self.clear_canvas)
        clear_btn.pack(side=tk.LEFT, padx=2)
        
        # 保存按钮
        save_btn = ttk.Button(toolbar, text="保存画作", command=self.save_drawing)
        save_btn.pack(side=tk.LEFT, padx=2)

    def choose_color(self):
        """选择画笔颜色"""
        color = colorchooser.askcolor(title="选择颜色")[1]
        if color:
            self.brush_color = color

    def update_brush_size(self):
        """更新画笔大小"""
        self.brush_size = self.size_var.get()

    def use_eraser(self):
        """使用橡皮擦（本质是白色画笔）"""
        self.brush_color = "white"
        self.brush_size = self.eraser_size

    def clear_canvas(self):
        """清空画布"""
        self.canvas.delete("all")

    def save_drawing(self):
        """保存画作（简单提示，实际保存需要额外库支持）"""
        tk.messagebox.showinfo("提示", "保存功能需要PIL库支持，\n请安装PIL库后实现完整功能")

    def start_drawing(self, event):
        """开始绘画"""
        self.is_drawing = True
        self.last_x = event.x
        self.last_y = event.y

    def draw(self, event):
        """绘画过程"""
        if self.is_drawing:
            # 绘制线条
            self.canvas.create_line(self.last_x, self.last_y, event.x, event.y,
                                  width=self.brush_size, fill=self.brush_color,
                                  capstyle=tk.ROUND, smooth=tk.TRUE)
            # 更新上一个点的坐标
            self.last_x = event.x
            self.last_y = event.y

    def stop_drawing(self, event):
        """停止绘画"""
        self.is_drawing = False
        self.last_x = None
        self.last_y = None

if __name__ == "__main__":
    root = tk.Tk()
    app = DrawingApp(root)
    root.mainloop()